<?php
namespace Common\Model;


class MessageBoxModel extends BaseModel
{
    protected $_auto = [
        ['created_at', 'time', self::MODEL_INSERT, 'function'],
    ];

    protected $_validate = [

    ];

    protected $_scope = [
        
    ];


    /**
     * 检查全站新信件
     *
     * @return void
     */
    public function pickGlobalMessage($uid)
    {
        $user = D('Users')->find($uid);
        $condition = [];
        if ($user['type'] == 1) {
            $condition['rec_type'] = 1;
        } elseif ($user['type'] == 2) { //主播
            $condition['rec_type'] = [['eq', 1], ['eq', 2], 'or'];
        }
        

        $hasIds = D('MessageBox')
            ->where(['user_id' => $uid])
            ->getField('msg_id', true);
        $msgIds = D('Message')
            ->where($condition)
            ->where(['deleted_at' => ['exp', 'is null']])
            ->getField('id', true);
        
        $hasIds = $hasIds ? : [];
        $msgIds = $msgIds ? : [];

        $diffIds = array_diff($msgIds, $hasIds);
        if ($diffIds) {
            $diffMessages = [];
            foreach ($diffIds as $diffId) {
                $diffMessages[] = [
                    'user_id' => $uid,
                    'msg_id' => $diffId,
                    'is_read' => 0,
                    'created_at' => time(),
                ];
            }
            D('MessageBox')->addAll($diffMessages);
        }

    }




}
